﻿#region

using System.ComponentModel;
using System.Linq;
using Radovici.Ctrls.Desktop;
using YouTubePlaylist.MusicVideoService;
using YouTubePlaylist.ViewModels;
using YouTubePlaylist.Views;

#endregion

namespace YouTubePlaylist.Models
{
    public class AppModel : DesktopModel
    {
        #region Private fields

        private readonly DataLayer _model;
        private readonly PlaylistsModel _myPlaylists;
        private readonly PlaylistsModel _createdPlaylists;
        private readonly PlaylistsModel _foundPlaylists;                
        private readonly PlaylistsModel _playingPlaylists;
        private readonly PlaylistsModel _updatedPlaylists;

        #endregion

        public AppModel()
        {
            _model = DataLayer.Singleton;
            _model.PropertyChanged += OnModelChanged;
            _myPlaylists = new PlaylistsModel(_model.MyPlaylists);
            _myPlaylists.Title = "My Playlists";
            _myPlaylists.PropertyChanged += OnPlaylistsChanged;
            _createdPlaylists = new PlaylistsModel(_model.MostRecentlyCreatedPlaylists);
            _createdPlaylists.Title = "Most Recent Playlists";
            _createdPlaylists.PropertyChanged += OnPlaylistsChanged;
            _updatedPlaylists = new PlaylistsModel(_model.MostRecentlyUpdatedPlaylists);
            _updatedPlaylists.Title = "Most Active Playlists";
            _updatedPlaylists.PropertyChanged += OnPlaylistsChanged;
            _playingPlaylists = new PlaylistsModel(_model.PlayingPlaylists);
            _playingPlaylists.Title = "Now Playing";
            _playingPlaylists.PropertyChanged += OnPlaylistsChanged;
            //http://msdn.microsoft.com/en-us/library/cc296240(VS.95).aspx
            base.Add(new MenuItemModel(this, "My Playlists",
                "/YouTubePlaylist;component/Views/Resources/48/itunes-48x48.png", 
                "View and edit your playlists",
                delegate { return new PlaylistsCtrl {PlaylistsModel = _myPlaylists}; }));
            base.Add(new MenuItemModel(this, "Finder", 
                "/YouTubePlaylist;component/Views/Resources/48/finder-48x48.png",
                "Find and add well-known songs",
                delegate { return new FinderCtrl(); }));
            base.Add(new MenuItemModel(this, "Add Song",
                "/YouTubePlaylist;component/Views/Resources/48/movie-track-add-48x48.png",
                "Manually add a song",
                delegate { return new TrackInputViewModel(new Track()).LoadView(); }));
        }

        public DataLayer Model
        {
            get { return _model; }
        }

        public PlaylistsModel MyPlaylists
        {
            get { return _myPlaylists; }
        }

        public PlaylistsModel MostRecentlyCreatedPlaylists
        {
            get { return _createdPlaylists; }
        }

        public PlaylistsModel MostRecentlyUpdatedPlaylists
        {
            get { return _updatedPlaylists; }
        }

        public PlaylistsModel PlayingPlaylists
        {
            get { return _playingPlaylists; }
        }

        public void Add(PlaylistModel playlist)
        {
            base.Add(new WidgetModel(
                         delegate { return new TracksCtrl {PlaylistModel = playlist}; }
                         ));
        }

        public void Add(VideoPlaylist playlist)
        {
            base.Add(new WidgetModel(
                         delegate { return new TracksCtrl {PlaylistModel = GetPlaylistModel(playlist)}; }
                         ));
        }

        private PlaylistModel GetPlaylistModel(VideoPlaylist playlist)
        {
            PlaylistModel model = _playingPlaylists.PlaylistModels.SingleOrDefault(p => p.Playlist == playlist);
            if (model == null)
            {
                model = _myPlaylists.PlaylistModels.SingleOrDefault(p => p.Playlist == playlist);
                if (model == null)
                {
                    model = new PlaylistModel(playlist);
                }
            }
            return model;
        }

        #region Private methods

        private void OnModelChanged(object sender, PropertyChangedEventArgs e)
        {
            switch (e.PropertyName)
            {
                case "User":
                    break;
                case "MyPlaylists":
                    break;
                case "MostRecentlyCreatedPlaylists":
                    break;
                case "MostRecentlyUpdatedPlaylists":
                    break;
                case "FoundPlaylists":
                    break;
                case "LinkedPlaylist":
                    break;
                case "LinkedTrack":
                    {
                        VideoPlaylist vp = _model.LinkedPlaylist;
                        if (vp != null)
                        {
                            PlaylistModel plVm = _playingPlaylists.PlaylistModels.SingleOrDefault(p => p.Playlist == vp); //NOTE: Need to get PlaylistModel; can't use contains.
                            if (plVm != null)
                            {
                                _playingPlaylists.Current = plVm;
                                VideoTrack vt = _model.LinkedTrack;
                                if (vt != null)
                                {
                                    if (plVm.Playlist.Tracks.Contains(vt))
                                    {
                                        plVm.Current = vt;
                                    }
                                }
                            }
                        }
                        break;
                    }
            }
            Notify(e.PropertyName);
        }

        private void OnPlaylistsChanged(object sender, PropertyChangedEventArgs e)
        {
            var viewModel = sender as PlaylistsModel;
            if (viewModel != null)
            {
                if (sender == _myPlaylists)
                {
                    Notify("MyPlaylists");
                }
                else if (sender == _createdPlaylists)
                {
                    Notify("MostRecentlyCreatedPlaylists");
                }
                else if (sender == _updatedPlaylists)
                {
                    Notify("MostRecentlyUpdatedPlaylists");
                }
                else if (sender == _playingPlaylists)
                {
                    Notify("PlayingPlaylists");
                }
            }
        }

        #endregion
    }
}